/**
 * 
 */
package com.solarroast.mirrorarray.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

import com.solarroast.mirrorarray.SolarRoastConstants;
import com.solarroast.mirrorarray.SolarRoastMirrorArray;
import com.solarroast.mirrorarray.beans.MAIBean;
import com.solarroast.mirrorarray.components.MirrorArray;
import com.solarroast.mirrorarray.components.MirrorArraySelection;
import com.solarroast.mirrorarray.gui.listeners.ActivateButtonListener;
import com.solarroast.mirrorarray.gui.listeners.ClearButtonListener;
import com.solarroast.mirrorarray.gui.listeners.DeactivateButtonListener;
import com.solarroast.mirrorarray.gui.listeners.DeleteButtonListener;
import com.solarroast.mirrorarray.gui.listeners.LoadButtonListener;
import com.solarroast.mirrorarray.gui.listeners.SaveButtonListener;
import com.solarroast.mirrorarray.gui.listeners.SelectAllButtonListener;

/**
 * @author baiken
 *
 */
public class MainWindow extends JFrame implements SolarRoastConstants {
	private static final long serialVersionUID = 6951018493162607752L;

	JLabel infoBar = new JLabel(" InfoBar");
	
	JLabel lat = new JLabel("Latitude: 40N");
	JLabel lon = new JLabel("Longitude: -105W");
	JLabel jDate = new JLabel("Julian Date: ");
	JLabel jCentury = new JLabel("Julian Century: ");
	JLabel jGeomMeanLong = new JLabel("Geom Mean Long Sun: ");
	JLabel jGeomMeanAnom = new JLabel("Geom Mean Anom Sun: ");
	JLabel jEccentEarth = new JLabel("Eccent Earth Orbit: ");
	JLabel jSunEqCtr = new JLabel("Sun Eq of Ctr: ");
	JLabel jSunTrueLong = new JLabel("Sun True Long: ");
	JLabel jSunTrueAnom = new JLabel("Sun True Anom: ");
	JLabel jSunRadVector = new JLabel("Sun Rad Vector: ");
	JLabel jSunAppLong = new JLabel("Sun App Long: ");
	JLabel jMeanObliqEclip = new JLabel("Mean Obliq Ecliptic: ");
	JLabel jObliqCorr = new JLabel("Obliq Corr: ");
	JLabel jSunDecline = new JLabel("Sun Decline: ");
	JLabel jVarY = new JLabel("Var Y: ");
	JLabel jEqOfTime = new JLabel("Eq of Time: ");
	JLabel jHASunrise = new JLabel("HA Sunrise: ");
	JLabel jSolarNoon = new JLabel("Solar Noon: ");
	JLabel jSunriseTime = new JLabel("Sunrise Time: ");
	JLabel jSunsetTime = new JLabel("Sunset Time: ");
	JLabel jSunlightDur = new JLabel("Sunlight Duration: ");
	JLabel jTrueSolarTime = new JLabel("True Solar Time: ");
	JLabel jHourAngle = new JLabel("Hour Angle: ");
	JLabel jSolarZenith = new JLabel("Solar Zenith Angle: ");
	JLabel jSolarElev = new JLabel("Solar Elevation Angle: ");
	
	MirrorArray mArray;
	ArrayList<MirrorArraySelection> selections = new ArrayList<MirrorArraySelection>();

	public MainWindow() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		initMainWindow();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		this.setVisible(true);
	}
	
	protected void initMainWindow() {
		mArray = new MirrorArray(MIRRORS_X_AXIS, MIRRORS_Y_AXIS, Double.valueOf(Math.pow(NUM_MIRRORS_SIDE, 2)).intValue());

		this.setSize(1000, 800);
		this.setResizable(false);
		this.setTitle("Solar Roast Mirror Array Controller");
		
		JTabbedPane tabs = new JTabbedPane();
		JPanel infoAreaPanel = new JPanel();
		JPanel mirrorAreaPanel = new JPanel();
		JPanel outputAreaPanel = new JPanel(new BorderLayout(0, 0));
		JPanel listAreaPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
		JPanel mirrorListPanel = new JPanel();
		mirrorListPanel.setPreferredSize(new Dimension(200, 400));
		mirrorListPanel.setBorder(BorderFactory.createTitledBorder("Mirror Selections"));
		listAreaPanel.setPreferredSize(new Dimension(200, 800));
		JPanel listAreaButtonsPanel = new JPanel();
		listAreaButtonsPanel.setLayout(new GridLayout(8, 1));
		
		JButton save = new JButton("Save Selection");
		JButton load = new JButton("Load Selection");
		JButton delete = new JButton("Delete Selection");
		JButton print = new JButton("Print Selections");
		JButton activate = new JButton("Activate Selection");
		JButton deactivate = new JButton("Deactivate Selection");
		JButton selectAll = new JButton("Select All");
		JButton clear = new JButton("Clear Selection");
		 
		final DefaultListModel model = new DefaultListModel();
		final JList mirrorAreaList = new JList(model);
		JScrollPane mirrorAreaListSP = new JScrollPane(mirrorAreaList);
		
		String[] listItems = new String[]{};
		
		for(int i = 0; i < listItems.length; i++) {
			model.add(i, listItems[i]);
		}
		
		mirrorAreaList.setName("Mirror Selections");
		mirrorAreaListSP.setPreferredSize(new Dimension(180, 350));
		
		listAreaButtonsPanel.add(save);
		listAreaButtonsPanel.add(load);
		listAreaButtonsPanel.add(delete);
		listAreaButtonsPanel.add(print);		// currently in for testing - quick output
		listAreaButtonsPanel.add(activate);
		listAreaButtonsPanel.add(deactivate);
		listAreaButtonsPanel.add(selectAll);
		listAreaButtonsPanel.add(clear);
		
		save.addActionListener(new SaveButtonListener(mArray, selections, model));
		load.addActionListener(new LoadButtonListener(mArray, mirrorAreaList, selections, model));
		clear.addActionListener(new ClearButtonListener(mArray));
		activate.addActionListener(new ActivateButtonListener(mArray));
		deactivate.addActionListener(new DeactivateButtonListener(mArray));
		delete.addActionListener(new DeleteButtonListener(mirrorAreaList, selections, model));
		selectAll.addActionListener(new SelectAllButtonListener(mArray));
		
		print.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("There are " + selections.size() + " selections.");
				
				for(int i = 0; i < selections.size(); i++) {
					System.out.println("Selection " + (i + 1) + " has items:");
					selections.get(i).printValues();
				}
			}
		});		
						
		mirrorListPanel.add(mirrorAreaListSP);
		listAreaPanel.add(mirrorListPanel);
		listAreaPanel.add(listAreaButtonsPanel);
		
		JMenuBar menubar = new JMenuBar();
		JMenu file = new JMenu("File");
		
		menubar.add(file);
		setJMenuBar(menubar);
		
		infoBar.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
		infoBar.setPreferredSize(new Dimension(-1, 22));
		infoBar.setFont(new Font("Verdana", Font.PLAIN, 11));
		
		add(infoBar, BorderLayout.SOUTH);
		
		infoAreaPanel.setLayout(new BoxLayout(infoAreaPanel, BoxLayout.PAGE_AXIS));
		infoAreaPanel.setBorder(BorderFactory.createTitledBorder("Info"));
		mirrorAreaPanel.setLayout(new GridLayout(MIRRORS_X_AXIS, MIRRORS_Y_AXIS));
		
		infoAreaPanel.add(lat);
		infoAreaPanel.add(lon);
		infoAreaPanel.add(jDate);
		infoAreaPanel.add(jCentury);
		infoAreaPanel.add(jGeomMeanLong);
		infoAreaPanel.add(jGeomMeanAnom);
		infoAreaPanel.add(jEccentEarth);
		infoAreaPanel.add(jSunEqCtr);
		infoAreaPanel.add(jSunTrueLong);
		infoAreaPanel.add(jSunTrueAnom);
		infoAreaPanel.add(jSunRadVector);
		infoAreaPanel.add(jSunAppLong);
		infoAreaPanel.add(jMeanObliqEclip);
		infoAreaPanel.add(jObliqCorr);
		infoAreaPanel.add(jSunDecline);
		infoAreaPanel.add(jVarY);
		infoAreaPanel.add(jEqOfTime);
		infoAreaPanel.add(jHASunrise);
		infoAreaPanel.add(jSolarNoon);
		infoAreaPanel.add(jSunriseTime);
		infoAreaPanel.add(jSunsetTime);
		infoAreaPanel.add(jSunlightDur);
		infoAreaPanel.add(jTrueSolarTime);
		infoAreaPanel.add(jHourAngle);
		infoAreaPanel.add(jSolarZenith);
		infoAreaPanel.add(jSolarElev);
				
		// TO IMPLEMENT
		// USE GRID LAYOUT AND IMPLEMENT BUTTONS FOR MIRRORS
		// MIGHT NEED SCROLL BARS OR ABILITY TO SELECT MIRRORS WITHIN SUB GROUPS TO
		// DISABLE INDIVIDUAL MIRRORS
		
		int numActive = 0;
		for(int r = 0; r < MIRRORS_Y_AXIS; r++) {
			for(int c = 0; c < MIRRORS_X_AXIS; c++) {
				mirrorAreaPanel.add(mArray.getMirrorArrayItem(r, c).getButton());
				numActive += mArray.getMirrorArrayItem(r, c).getActive();
			}
		}
		
		System.out.println("There are " + mArray.getNumMirrors() + " mirrors total, of which " + numActive + " are active.");
		
		outputAreaPanel.add(listAreaPanel, BorderLayout.WEST);
		outputAreaPanel.add(mirrorAreaPanel, BorderLayout.CENTER);
		tabs.add(outputAreaPanel, "Mirror Array");
		tabs.add(infoAreaPanel, "Calculations");
		add(tabs, BorderLayout.CENTER);
		
		runClockFeed();
		
	}
		
	public void runClockFeed() {
		class CalcUpdater implements Runnable {
			public void run() {
				while(true) {
					infoBar.setText(" Infobar: " + SolarRoastMirrorArray.clock.getTime());
					
					if(SolarRoastMirrorArray.clock.getSecond() % 2 == 0) {
						jDate.setText("Julian Date: " + SolarRoastMirrorArray.clock.getJDN());
						jCentury.setText("Julian Century: " + SolarRoastMirrorArray.clock.getJCent());
						jGeomMeanLong.setText("Geom Mean Long Sun: " + SolarRoastMirrorArray.clock.getGeomMeanLong());
						jGeomMeanAnom.setText("Geom Mean Anom Sun: " + SolarRoastMirrorArray.clock.getGeomMeanAnom());
						jEccentEarth.setText("Eccent Earth Orbit: " + SolarRoastMirrorArray.clock.getEccentEarthOrbit());
						jSunEqCtr.setText("Sun Eq of Ctr: " + SolarRoastMirrorArray.clock.getSunEqCtr());
						jSunTrueLong.setText("Sun True Long: " + SolarRoastMirrorArray.clock.getSunTrueLong());
						jSunTrueAnom.setText("Sun True Anom: " + SolarRoastMirrorArray.clock.getSunTrueAnom());
						jSunAppLong.setText("Sun App Long: " + SolarRoastMirrorArray.clock.getSunAppLong());
						jSunRadVector.setText("Sun Rad Vector: " + SolarRoastMirrorArray.clock.getSunRadVector());
						jMeanObliqEclip.setText("Mean Obliq Ecliptic: " + SolarRoastMirrorArray.clock.getMeanObliqEclip());
						jObliqCorr.setText("Obliq Corr: " + SolarRoastMirrorArray.clock.getObliqCorr());
						jSunDecline.setText("Sun Decline: " + SolarRoastMirrorArray.clock.getSunDecline());
						jVarY.setText("Var Y: " + SolarRoastMirrorArray.clock.getVarY());
						jEqOfTime.setText("Eq of Time: " + SolarRoastMirrorArray.clock.getEqOfTime());
						jHASunrise.setText("HA Sunrise: " + SolarRoastMirrorArray.clock.getHaSunrise());
						jSolarNoon.setText("Solar Noon: " + SolarRoastMirrorArray.clock.getSolarNoon());
						jSunriseTime.setText("Sunrise Time: " + SolarRoastMirrorArray.clock.getSunriseTime());
						jSunsetTime.setText("Sunset Time: " + SolarRoastMirrorArray.clock.getSunsetTime());
						jSunlightDur.setText("Sunlight Duration: " + SolarRoastMirrorArray.clock.getSunlightDur());
						jTrueSolarTime.setText("True Solar Time: " + SolarRoastMirrorArray.clock.getTrueSolarTime());
						jHourAngle.setText("Hour Angle: " + SolarRoastMirrorArray.clock.getHourAngle());
						jSolarZenith.setText("Solar Zenith Angle: " + SolarRoastMirrorArray.clock.getSolarZenithAngle());
						jSolarElev.setText("Solar Elevation Angle: " + SolarRoastMirrorArray.clock.getSolarElevAngle());
					}
					
					try {
						Thread.sleep(1000);
					}
					catch(Exception e) {
						e.printStackTrace();
					}
				}
			}
		};
		
		CalcUpdater calcThread = new CalcUpdater();
		new Thread(calcThread).start();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
